package com.cx.file.controller;

import com.cx.file.model.Result;
import com.cx.file.service.FileManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author cx
 * @description: 文件管理
 * @date 2023/6/6 11:13
 */
@Api(value = "api/v1/file", tags = "文件管理")
@RestController
@RequestMapping(value = "/api/v1/file")
public class FileManageController {

    @Autowired
    private FileManageService fileManageService;

    @ApiOperation(value = "上传")
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam(required = false) String directory, @RequestBody MultipartFile file) throws IOException {
        return Result.success(fileManageService.uploadFile(directory, file));
    }

    @ApiOperation(value = "下载")
    @GetMapping("/download")
    public void downloadFile(@RequestParam String path, HttpServletResponse response) {
        fileManageService.downloadFile(path, response);
    }

    @ApiOperation(value = "下载zip")
    @PostMapping("/download/zip")
    public void downloadFile(@RequestBody List<String> filePaths, HttpServletResponse response) throws IOException {
        fileManageService.downloadFileZip(filePaths, response);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping()
    public Result<Object> deleteFile(@RequestParam String path) {
        fileManageService.deleteFile(path);
        return Result.success();
    }
}
